﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAL;

namespace BLL.Article
{
    public class Comment : BaseArticle
    {
        #region "Constructors"
        public Comment(int id, DateTime addedDate, string addedBy, string addedByEmail,
           string addedByIP, int articleID, string articleTitle, string body)
        {
            this.ID = id;
            this.AddedDate = addedDate;
            this.AddedBy = addedBy;
            this.AddedByEmail = addedByEmail;
            this.AddedByIP = addedByIP;
            this.ArticleID = articleID;
            this.ArticleTitle = articleTitle;
            this.Body = body;
        }
        #endregion

        #region "Public Properties"
        public string AddedByEmail { get; set; }

        public string AddedByIP { get; set; }

        public int ArticleID { get; set; }

        public string ArticleTitle { get; set; }

        public string Body { get; set; }

        public string EncodedBody
        {
            get { return Helpers.ConvertToHtml(this.Body); }
        }
        #endregion

        #region Get From CommentDetail
        private static Comment GetCommentFromCommentDetails(CommentDetails record)
        {
            if (record == null)
                return null;
            else
            {
                return new Comment(record.ID, record.AddedDate, record.AddedBy,
                   record.AddedByEmail, record.AddedByIP,
                   record.ArticleID, record.ArticleTitle, record.Body);
            }
        }

        private static List<Comment> GetCommentListFromCommentDetailsList(List<CommentDetails> recordset)
        {
            List<Comment> comments = new List<Comment>();
            foreach (CommentDetails record in recordset)
                comments.Add(GetCommentFromCommentDetails(record));
            return comments;
        }
        #endregion

        #region Static Method
        public static List<Comment> ListComments(int startRowIndex, int maximumRows)
        {
            List<Comment> comments = null;
            string key = "Comments_" + startRowIndex.ToString() + "_" + maximumRows.ToString();

            if (BaseArticle.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                comments = (List<Comment>)BizObject.Cache[key];
            }
            else
            {
                List<CommentDetails> recordset = SiteProvider.Articles.ListComments(
                maximumRows, GetPageIndex(startRowIndex, maximumRows));
                comments = GetCommentListFromCommentDetailsList(recordset);
                BaseArticle.CacheData(key, comments);
            }
            return comments;
        }

        public static List<Comment> ListComments(int articleID)
        {
            List<Comment> comments = null;
            string key = "Comments_Article_" + articleID.ToString();

            if (BaseArticle.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                comments = (List<Comment>)BizObject.Cache[key];
            }
            else
            {
                List<CommentDetails> recordset = SiteProvider.Articles.ListCommentByArticle(articleID,0,BizObject.MAXROWS);
                comments = GetCommentListFromCommentDetailsList(recordset);
                BaseArticle.CacheData(key, comments);
            }
            return comments;
        }

        public static List<Comment> ListComments(int articleID, int startRowIndex, int maximumRows)
        {
            List<Comment> comments = null;
            string key = "Comments_Article_" + articleID.ToString() + "_" + startRowIndex.ToString() + "_" + maximumRows.ToString();

            if (BaseArticle.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                comments = (List<Comment>)BizObject.Cache[key];
            }
            else
            {
                List<CommentDetails> recordset = SiteProvider.Articles.ListCommentByArticle(articleID,
                GetPageIndex(startRowIndex, maximumRows), maximumRows);
                comments = GetCommentListFromCommentDetailsList(recordset);
                BaseArticle.CacheData(key, comments);
            }
            return comments;
        }

        public static int GetCommentCount()
        {
            int commentCount = 0;
            string key = "Comment_CommentCount";

            if (BaseArticle.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                commentCount = (int)BizObject.Cache[key];
            }
            else
            {
                commentCount = SiteProvider.Articles.GetCommentCount();
                BaseArticle.CacheData(key, commentCount);
            }
            return commentCount;
        }

        public static int GetCommentCountByArticle(int articleID)
        {
            int commentCount = 0;
            string key = "Comment_CommentCount_Article_" + articleID.ToString();

            if (BaseArticle.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                commentCount = (int)BizObject.Cache[key];
            }
            else
            {
                commentCount = SiteProvider.Articles.GetCommentCountByArticle(articleID);
                BaseArticle.CacheData(key, commentCount);
            }
            return commentCount;
        }

        public static Comment GetCommentByID(int commentID)
        {
            Comment comment = null;
            string key = "Comment_" + commentID.ToString();

            if (BaseArticle.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                comment = (Comment)BizObject.Cache[key];
            }
            else
            {
                comment = GetCommentFromCommentDetails(SiteProvider.Articles.GetCommentByID(commentID));
                BaseArticle.CacheData(key, comment);
            }
            return comment;
        }

        public static bool UpdateComment(int id, string body)
        {
            CommentDetails record = new CommentDetails(
               id, DateTime.Now, "", "", "", 0,"", body);
            bool ret = SiteProvider.Articles.UpdateComment(record);
            BizObject.PurgeCacheItems("Comment");
            return ret;
        }

        public static bool DeleteComment(int id)
        {
            bool ret = SiteProvider.Articles.DeleteComment(id);
            //new RecordDeletedEvent("comment", id, null).Raise();
            BizObject.PurgeCacheItems("Comment");
            return ret;
        }

        public static int InsertComment(string addedBy, string addedByEmail, int articleID, string body)
        {
            CommentDetails record = new CommentDetails(0, DateTime.Now, addedBy, addedByEmail, BizObject.CurrentUserIP, articleID,"", body);
            int ret = SiteProvider.Articles.InsertComment(record);
            BizObject.PurgeCacheItems("Comment");
            return ret;
        }
        #endregion

        #region Method

        public bool Delete()
        {
            bool success = Comment.DeleteComment(this.ID);
            if (success)
                this.ID = 0;
            return success;
        }

        public bool Update()
        {
            return Comment.UpdateComment(this.ID, this.Body);
        }
        #endregion
    }
}
